<?php

use think\facade\Db;

class Model_Magic extends PhalApi_Model_NotORM
{
	
	/* 配置 */
	public function getConfig()
	{
		$list = DI()->notorm->magic_con->order('id asc')->fetchAll();
		
		return $list;
	}
	
	/* 奖品信息 */
	public function getMagics()
	{
		$list = DI()->notorm->magic->order('id asc')->fetchAll();
		foreach($list as &$item){
			$item['all_type'] = explode(',', $item['types']);
		}
		
		return $list;
	}
	
	/* 转盘纪录 */
	public function setlog($data)
	{
		$rs = DI()->notorm->magic_log->insert($data);
		
		return $rs;
	}
	
	/* 转盘纪录 */
	public function uplogwin($id, $iswin)
	{
		$rs = DI()->notorm->magic_log->where('id=?', $id)->update(['iswin' => $iswin]);
		
		return $rs;
	}
	
	/* 添加中奖纪录 */
	public function setWin($data)
	{
		$rs = DI()->notorm->magic_win->insert($data);
		
		return $rs;
	}
	
	/* 中奖纪录 */
	public function getWin($uid, $p, $pnum = 50)
	{
		if($p < 1){
			$p = 1;
		}
		
		//$pnum  = 50;
		$start = ($p - 1) * $pnum;
		if($uid){
			$list = DI()->notorm->magic_win
				->where('uid=?', $uid)->order('id desc')->limit($start, $pnum)->fetchAll();
		} else{
			$list = DI()->notorm->magic_win->order('id desc')->limit($start, $pnum)->fetchAll();
		}
		
		
		return $list;
	}
	
	// 召唤记录
	public function myMagic($uid, $p, $pnum = 50)
	{
		if($p < 1){
			$p = 1;
		}
		
		$start = ($p - 1) * $pnum;
		$list  = DI()->notorm->magic_log
			->where('uid=?', $uid)->order('id desc')->limit($start, $pnum)->fetchAll();
		if(empty($list)){
			return $list;
		}
		
		// 中奖记录
		$logIds  = array_column($list, 'id');
		$winlist = DI()->notorm->magic_win
			->where('logid in ('.implode(',', $logIds).')')->order('id desc')->fetchAll();
		
		// 礼物
		$gifts = $this->getAllGift();
		$gifts = array_column($gifts, null, 'id');
		// 中奖礼物整理
		$wins = [];
		foreach($winlist as $key => $_win){
			$giftid   = $_win['giftid'];
			$giftInfo = $gifts[$giftid] ?? [];
			// 整理
			$_win['giftname'] = $giftInfo['giftname'] ?? '';
			$_win['gifticon'] = get_upload_path($giftInfo['gifticon'] ?? '');
			// 在整理
			$wins[$_win['logid']][] = $_win;
		}
		
		// 整理
		foreach($list as $key => $item){
			$list[$key]['win'] = $wins[$item['id']] ?? [];
		}
		
		return $list;
	}
	
	// 召唤记录:中奖的
	public function getWinMagic($uid, $p = 1, $pnum = 50)
	{
		if($p < 1){
			$p = 1;
		}
		
		$start = ($p - 1) * $pnum;
		if($uid){
			$list = DI()->notorm->magic_log
				->where('uid=? and iswin=1', $uid)->order('id desc')->limit($start, $pnum)->fetchAll();
		} else{
			$list = DI()->notorm->magic_log
				->where('iswin=1')->order('id desc')->limit($start, $pnum)->fetchAll();
		}
		if(empty($list)){
			return $list;
		}
		
		// 中奖记录
		$logIds  = array_column($list, 'id');
		$winlist = DI()->notorm->magic_win
			->where('logid in ('.implode(',', $logIds).')')->order('id desc')->fetchAll();
		
		// 礼物
		$gifts = $this->getAllGift();
		$gifts = array_column($gifts, null, 'id');
		// 中奖礼物整理
		$wins = [];
		foreach($winlist as $key => $_win){
			$giftid   = $_win['giftid'];
			$giftInfo = $gifts[$giftid] ?? [];
			// 整理
			$_win['giftname'] = $giftInfo['giftname'] ?? '';
			$_win['gifticon'] = get_upload_path($giftInfo['gifticon'] ?? '');
			// 在整理
			$wins[$_win['logid']][] = $_win;
		}
		
		// 整理
		foreach($list as $key => $item){
			$list[$key]['user'] = getUserInfo($item['uid']);
			$list[$key]['win']  = $wins[$item['id']] ?? [];
		}
		
		return [$list];
	}
	
	// 获取所有的礼物列表
	public function getAllGift()
	{
		return DI()->notorm->gift->select('id,giftname,gifticon')->fetchAll();
	}
}

